package com.yubest.tool.manage;

import com.google.gson.Gson;
import com.yubest.device.util.*;
import de.schlichtherle.license.*;
import de.schlichtherle.xml.GenericCertificate;
import lombok.Setter;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

/**
 * @Author hweiyu
 * @Description
 * @Date 2022/6/15 17:57
 */
@Setter
public class MyLicenseManager extends LicenseManager {

    private boolean creator;

    public MyLicenseManager(LicenseParam param) {
        super(param);
    }

    @Override
    protected synchronized LicenseContent verify(final LicenseNotary notary)
            throws Exception {
        GenericCertificate certificate = getCertificate();
        if (null != certificate)
            return (LicenseContent) certificate.getContent();

        // Load license key from preferences,
        final byte[] key = getLicenseKey();
        if (null == key)
            throw new NoLicenseInstalledException(getLicenseParam().getSubject());
        certificate = getPrivacyGuard().key2cert(key);
        notary.verify(certificate);
        //重写获取内容的方法
        final LicenseContent content = this.readXml(certificate.getEncoded());
        this.validate(content);
        setCertificate(certificate);

        return content;
    }

    private LicenseContent readXml(String encoded) throws Exception {
        try (BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(encoded.getBytes(StandardCharsets.UTF_8)));
             XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(inputStream, 4096), null, null)) {
            return (LicenseContent) decoder.readObject();
        }
    }

    @Override
    protected synchronized void validate(final LicenseContent content)
            throws LicenseContentException {
        super.validate(content);
        if (creator) {
            return;
        }
        String deviceJson = (String) content.getExtra();
        if (isNull(deviceJson)) {
            return;
        }
        DeviceInfo localDevice = null;
        try {
            localDevice = DeviceUtil.getDeviceInfo();
        } catch (Exception e) {
            throw new LicenseContentException("无法获取当前服务器设备信息");
        }
        if (null == localDevice) {
            throw new LicenseContentException("无法获取当前服务器设备信息");
        }
        DeviceInfo licDevice = new Gson().fromJson(deviceJson, DeviceInfo.class);
        boolean isCpuOk = true;
        if (!isNull(licDevice.getCpu())) {
            isCpuOk = licDevice.getCpu().equals(localDevice.getCpu());
        }
        boolean isMainBoardOk = true;
        if (!isNull(licDevice.getMainBoard())) {
            isMainBoardOk = licDevice.getMainBoard().equals(localDevice.getMainBoard());
        }
        boolean isIpOk = true;
        if (!isNull(licDevice.getIpList())) {
            isIpOk = licDevice.getIpList().removeAll(localDevice.getIpList());
        }
        boolean isMacOk = true;
        if (!isNull(licDevice.getMacList())) {
            isMacOk = licDevice.getMacList().removeAll(localDevice.getMacList());
        }

        if (!isCpuOk || !isMainBoardOk || !isIpOk || !isMacOk) {
            throw new LicenseContentException("当前服务器不在授权范围内");
        }
    }

    private boolean isNull(String str) {
        return null == str || "".equals(str.trim());
    }

    private boolean isNull(List list) {
        return null == list || list.isEmpty();
    }

}
